package com.lsht.web.controller.siwei;

import com.github.pagehelper.PageInfo;
import com.lsht.common.bo.IdListAcceptBo;
import com.lsht.common.core.controller.BaseController;
import com.lsht.common.core.domain.AjaxResult;
import com.lsht.common.core.domain.model.LoginUser;
import com.lsht.common.utils.ServletUtils;
import com.lsht.common.utils.poi.ExcelUtil;
import com.lsht.framework.web.service.TokenService;
import com.lsht.system.domain.siwei.SiweiDevice;
import com.lsht.system.domain.siwei.SiweiPoint;
import com.lsht.system.domain.siwei.SiweiPointDetails;
import com.lsht.system.service.siwei.SiweiPointDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 点位记录管理Controller
 *
 * @author zyc
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/siwei/details")
@Api(value = "SiweiPointDetails", description = "点位记录管理", tags = "点位记录管理")
public class SiweiPointDetailsController extends BaseController {
    @Autowired
    private SiweiPointDetailsService siweiPointDetailsService;
    @Autowired
    private TokenService tokenService;

    /**
     * 新增点位记录管理
     */
    @PostMapping(ADD_FUNCTION_URL)
    @ApiOperation(value = ADD_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "点位记录管理")
    public AjaxResult genAdd(SiweiPointDetails siweiPointDetails, List<MultipartFile> file) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return toAjax(siweiPointDetailsService.genAdd(siweiPointDetails, file, loginUser));
    }

    /**
     * 新增点位记录管理app端使用
     */
    @PostMapping("addPointDetailsByApp")
    @ApiOperation(value = "新增点位记录管理app端使用", httpMethod = "POST", response = AjaxResult.class, tags = "点位记录管理")
    public AjaxResult addPointDetailsByApp(@RequestBody SiweiPointDetails siweiPointDetails) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return toAjax(siweiPointDetailsService.addPointDetailsByApp(siweiPointDetails, loginUser));
    }

    /**
     * 删除点位记录管理
     */
    @PostMapping(DELETE_BY_ID_FUNCTION_URL)
    @ApiOperation(value = DELETE_BY_ID_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "点位记录管理")
    public AjaxResult genDeleteById(@RequestBody IdListAcceptBo idListAcceptBo) {
        List<String> idList = idListAcceptBo.getIdList();
        if (CollectionUtils.isNotEmpty(idList)) {
            return toAjax(siweiPointDetailsService.genDeleteById(idList));
        }
        return AjaxResult.error();
    }

    /**
     * 修改点位记录管理
     */
    @PostMapping(UPDATE_BY_ID_FUNCTION_URL)
    @ApiOperation(value = UPDATE_BY_ID_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "点位记录管理")
    public AjaxResult genUpdateById(@RequestBody SiweiPointDetails siweiPointDetails) {
        return toAjax(siweiPointDetailsService.genUpdateById(siweiPointDetails));
    }

    /**
     * 查询点位记录管理列表
     */
    @PostMapping(QUERY_LIST_FUNCTION_URL)
    @ApiOperation(value = QUERY_LIST_FUNCTION, httpMethod = "POST", response = SiweiPointDetails.class, tags = "点位记录管   理")
    public AjaxResult genQueryList(@RequestBody SiweiPointDetails siweiPointDetails) {
        startPage();
        List<SiweiPointDetails> list = siweiPointDetailsService.genQueryList(siweiPointDetails);
        return AjaxResult.success(new PageInfo<>(list));
    }

    /**
     * 根据id查询点位记录详情信息
     */
    @PostMapping(QUERY_BY_ID_FUNCTION_URL)
    @ApiOperation(value = QUERY_BY_ID_FUNCTION, httpMethod = "POST", response = SiweiDevice.class, tags = "点位记录管理")
    public AjaxResult genQueryById(@RequestBody SiweiPointDetails siweiPointDetails) {
        SiweiPointDetails pointDetails = siweiPointDetailsService.genQueryById(siweiPointDetails);
        return AjaxResult.success(pointDetails);
    }

    /**
     * 大屏查询填报记录为异常的数据
     */
    @PostMapping("pointDetailsByAbnormal")
    @ApiOperation(value = "大屏查询填报记录为异常的数据", httpMethod = "POST", response = SiweiDevice.class, tags = "点位记录管理")
    public AjaxResult pointDetailsByAbnormal(@RequestBody SiweiPointDetails siweiPointDetails) {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        boolean admin = loginUser.getUser().isAdmin();
//        if (admin) {
//            siweiPointDetails.setDeptId(null);
//        } else {
//            //获取部门ID
//            Long deptId = loginUser.getUser().getDeptId();
//            siweiPointDetails.setDeptId(deptId);
//        }
        startPage();
        List<SiweiPointDetails> pointDetails = siweiPointDetailsService.pointDetailsByAbnormal(siweiPointDetails);
        return AjaxResult.success(new PageInfo<>(pointDetails));
    }

    /**
     * 巡检记录查询
     */
    @PostMapping("inspectionRecord")
    @ApiOperation(value = "巡检记录查询", httpMethod = "POST", response = SiweiDevice.class, tags = "点位记录管理")
    public AjaxResult inspectionRecord(@RequestBody SiweiPointDetails siweiPointDetails) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        boolean admin = loginUser.getUser().isAdmin();
        if (admin) {
            siweiPointDetails.setDeptId(null);
        } else {
            //获取部门ID
            Long deptId = loginUser.getUser().getDeptId();
            siweiPointDetails.setDeptId(deptId);
        }
        Long userId = loginUser.getUserId();
        siweiPointDetails.setUserId(userId);
        startPage();
        List<SiweiPointDetails> pointDetails = siweiPointDetailsService.inspectionRecord(siweiPointDetails);
        return AjaxResult.success(new PageInfo<>(pointDetails));
    }

    /**
     * 新增记录视频
     */
    @PostMapping("addDetailsVideo")
    @ApiOperation(value = "新增记录视频", httpMethod = "POST", response = AjaxResult.class, tags = "点位记录管理")
    public AjaxResult addDetailsVideo(MultipartFile file) {
        return AjaxResult.success(siweiPointDetailsService.addDetailsVideo(file));
    }


    /**
     * 设备管理导出
     *
     * @param response
     * @param
     */
    @PostMapping("/exportDetails")
    @ApiOperation(value = "巡检记录查询导出", httpMethod = "POST", response = SiweiPoint.class, tags = "点位记录管理")
    public void exportDetails(HttpServletResponse response, SiweiPointDetails siweiPointDetails) {
        List<SiweiPointDetails> pointDetails = siweiPointDetailsService.inspectionRecord(siweiPointDetails);
        ExcelUtil<SiweiPointDetails> util = new ExcelUtil<SiweiPointDetails>(SiweiPointDetails.class);
        util.exportExcel(response, pointDetails, "巡检记录");
    }

}
